package com.lagou.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lagou.entity.User;
import com.lagou.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.sql.Wrapper;

/**
 * 创建人: yyn
 * 创建时间:2021/9/25 15:47
 */
@Controller
public class LaGouController {
    @Value("${server.port}")
    private int port;

    @Autowired
    private IUserService iUserService;

    @RequestMapping("/")
    public ModelAndView toLogin(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        modelAndView.addObject("port",port);
        return modelAndView;
    }

    @PostMapping("/login")
    public ModelAndView login(User user, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        modelAndView.addObject("port",port);
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("username",user.getUsername());
        User selectOne = iUserService.selectOne(wrapper);
        if(ObjectUtils.isEmpty(selectOne)){
            System.out.println("login");
            return modelAndView;
        }
        if(selectOne.getPassword().equals(user.getPassword())){
            System.out.println("index");
            modelAndView.setViewName("index");
            session.setAttribute(selectOne.getUsername(),user);
            return modelAndView;
        }
        return modelAndView;
    }
}
